package com.salesys.gr.gui;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;
import com.salesys.gr.util.FrmHelper;
import com.salesys.gr.util.MsgBox;
import com.salesys.gr.util.LoginedUserHelper;
import com.salesys.tony.domain.User;
import com.salesys.tony.service.UserService;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.events.TraverseEvent;

public class FrmModifyInfo2 {

	protected Shell FrmModifyInfo2;
	private Text txtNickName;
	private Text txtMark;
	private Text txtMemberId;
	private Text txtBankcard;
	private Text txtEmail;
	private Text txtQq;
	private User operatingUser;

	public void transfer(User modifyUser) {
		operatingUser = new User(modifyUser);
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		FrmModifyInfo2.open();
		FrmModifyInfo2.layout();
		while (!FrmModifyInfo2.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 * @wbp.parser.entryPoint
	 */
	protected void createContents() {
		FrmModifyInfo2 = new Shell(SWT.CLOSE | SWT.APPLICATION_MODAL);
		FrmModifyInfo2.addTraverseListener(new TraverseListener() {
			public void keyTraversed(TraverseEvent e) {
				if (e.keyCode == 27) {
					FrmModifyInfo2.close();
				}
			}
		});
		FrmModifyInfo2.setSize(300, 275);
		FrmModifyInfo2.setText("修改个人资料");
		// 窗口居中
		FrmModifyInfo2.setLocation(new FrmHelper().setLocationCenter(FrmModifyInfo2));

		Label lblNickName = new Label(FrmModifyInfo2, SWT.NONE);
		lblNickName.setBounds(40, 25, 60, 17);
		lblNickName.setText("姓　名：");

		Label lblMark = new Label(FrmModifyInfo2, SWT.NONE);
		lblMark.setBounds(40, 50, 60, 17);
		lblMark.setText("现　金：");

		Label lblMemberId = new Label(FrmModifyInfo2, SWT.NONE);
		lblMemberId.setBounds(40, 75, 60, 17);
		lblMemberId.setText("加盟卡号：");

		Label lblBankcard = new Label(FrmModifyInfo2, SWT.NONE);
		lblBankcard.setBounds(40, 100, 60, 17);
		lblBankcard.setText("银行账号：");

		Label lblEmail = new Label(FrmModifyInfo2, SWT.NONE);
		lblEmail.setBounds(40, 125, 60, 17);
		lblEmail.setText("邮　箱：");

		Label lblQq = new Label(FrmModifyInfo2, SWT.NONE);
		lblQq.setBounds(40, 150, 60, 17);
		lblQq.setText("QQ：");

		txtNickName = new Text(FrmModifyInfo2, SWT.BORDER);
		txtNickName.setBounds(102, 25, 150, 20);

		txtMark = new Text(FrmModifyInfo2, SWT.BORDER);
		txtMark.setBounds(102, 50, 150, 20);

		txtMemberId = new Text(FrmModifyInfo2, SWT.BORDER);
		txtMemberId.setBounds(102, 75, 150, 20);

		txtBankcard = new Text(FrmModifyInfo2, SWT.BORDER);
		txtBankcard.setBounds(102, 100, 150, 20);

		txtEmail = new Text(FrmModifyInfo2, SWT.BORDER);
		txtEmail.setBounds(102, 125, 150, 20);

		txtQq = new Text(FrmModifyInfo2, SWT.BORDER);
		txtQq.setBounds(102, 150, 150, 20);

		LoginedUserHelper.fresh();

		if (operatingUser.getNickname() != null) {
			txtNickName.setText(operatingUser.getNickname());
		}
		if (operatingUser.getMark() != null) {
			txtMark.setText(operatingUser.getMark());
		}
		if (operatingUser.getMemberId() != null) {
			txtMemberId.setText(operatingUser.getMemberId());
		}
		if (operatingUser.getBankcard() != null) {
			txtBankcard.setText(operatingUser.getBankcard());
		}
		if (operatingUser.getEmail() != null) {
			txtEmail.setText(operatingUser.getEmail());
		}
		if (operatingUser.getQq() != null) {
			txtQq.setText(operatingUser.getQq());
		}

		// 修改个人资料
		Button btnModify = new Button(FrmModifyInfo2, SWT.NONE);
		btnModify.addTraverseListener(new TraverseListener() {
			public void keyTraversed(TraverseEvent arg0) {
				if (arg0.keyCode == 13) {
					modifyInfo();
				}
			}
		});
		btnModify.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				modifyInfo();
			}
		});
		btnModify.setBounds(197, 205, 88, 30);
		btnModify.setText("修　改");

		Label lblTip = new Label(FrmModifyInfo2, SWT.NONE);
		lblTip.setBounds(23, 180, 262, 17);
		lblTip.setText("注：银行账号姓名必须与该系统账户姓名一致！");
	}

	public void modifyInfo() {
		User modifyInfoUser = new User(operatingUser);
		modifyInfoUser.setNickname(txtNickName.getText());
		modifyInfoUser.setMark(txtMark.getText());
		modifyInfoUser.setMemberId(txtMemberId.getText());
		modifyInfoUser.setBankcard(txtBankcard.getText());
		modifyInfoUser.setEmail(txtEmail.getText());
		modifyInfoUser.setQq(txtQq.getText());
		if (modifyInfoUser.getMark() == "") {
			new MsgBox().errorTip(FrmModifyInfo2, "现金不能为空");
		} else {
			try {
				if (new UserService().queryInfoFromMemberId(txtMemberId.getText()).getMemberId() != null && operatingUser.getMemberId().equals(modifyInfoUser.getMemberId()) == false) {
					new MsgBox().errorTip(FrmModifyInfo2, "该会员卡号已存在");
				} else {
					int affect1 = 0;
					int affect2 = 0;
					int affect3 = 0;
					int affect4 = 0;
					try {
						affect1 = new UserService().modifyInfo(modifyInfoUser);
					} catch (Exception e) {
						e.printStackTrace();
					}
					if (operatingUser.getMemberId() == null || operatingUser.getMemberId().equals(modifyInfoUser.getMemberId()) == false) {
						try {
							affect2 = new UserService().modifyMemberId(modifyInfoUser.getMemberId(), operatingUser.getUsername());
							if (affect2 == 1) {
								new UserService().autoChangeMarkForParent(operatingUser.getUsername());
							}
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
					if (modifyInfoUser.getMemberId() != "" && operatingUser.getMark().equals(modifyInfoUser.getMark()) == false) {
						if (Integer.parseInt(modifyInfoUser.getMark()) > Integer.parseInt(operatingUser.getMark())) {
							try {
								affect3 = new UserService().changeMark(operatingUser.getUsername(), operatingUser.getMark(), modifyInfoUser.getMark());
								affect4 = new UserService().changeMarkForParent(operatingUser.getUsername(), operatingUser.getMark(), modifyInfoUser.getMark());
							} catch (Exception e) {
								e.printStackTrace();
							}
						} else if (Integer.parseInt(modifyInfoUser.getMark()) < Integer.parseInt(operatingUser.getMark())) {
							try {
								affect3 = new UserService().changeMark(operatingUser.getUsername(), operatingUser.getMark(), modifyInfoUser.getMark());
								affect4 = 1;
							} catch (Exception e) {
								e.printStackTrace();
							}
						} else {
							affect3 = 1;
							affect4 = 1;
						}
					}
					if (affect1 == 0 && affect2 == 0 && affect3 == 0 && affect4 == 0) {
						new MsgBox().errorTip(FrmModifyInfo2, "修改失败！");
					} else if (affect1 == 1 && affect3 == 0 && affect4 == 0 && operatingUser.getMemberId() == null && modifyInfoUser.getMemberId() != null && operatingUser.getMark().equals(modifyInfoUser.getMark()) == false) {
						new MsgBox().infoTip(FrmModifyInfo2, "修改成功，但该用户还不是正式会员，无法变更余额！");
					} else {
						new MsgBox().infoTip(FrmModifyInfo2, "修改成功！");
						FrmModifyInfo2.close();
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}